package org.wolfengi.monitor.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.wolfengi.monitor.common.ShipCN;
import org.wolfengi.monitor.entity.FShipFaultTypeCount;
import org.wolfengi.monitor.mapper.FShipFaultTypeCountMapper;
import org.wolfengi.monitor.service.FShipFaultTypeCountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wolfengi
 * @since 2020-05-21
 */
@Service
public class FShipFaultTypeCountServiceImpl extends ServiceImpl<FShipFaultTypeCountMapper, FShipFaultTypeCount> implements FShipFaultTypeCountService {

    @Autowired
    private FShipFaultTypeCountMapper fShipFaultTypeCountMapper;

    /**
     * 参照出行率的堆叠图分析设计，构建独特的数据结构
     *
     * @return
     */
    @Override
    public Map setfShipFaultType() {
        List<Map> datas = fShipFaultTypeCountMapper.selectShipFaultTypeCount();
        List<String> faultTypeListold = fShipFaultTypeCountMapper.selectShipFaultType();
        List<String> shipTypeList = fShipFaultTypeCountMapper.selectShipType();

        List<String> shipsCN = new ArrayList<>(shipTypeList.size());
        List<String> faultsCN = new ArrayList<>(faultTypeListold.size());

        shipTypeList.forEach(l -> shipsCN.add(ShipCN.getCNTrim(l)));
        List<String> faultTypeList = new ArrayList<>();
        faultTypeListold.forEach(l -> {
            String cn = ShipCN.getCNTrim(l);
            if (null != cn) {
                faultsCN.add(cn);
                /**
                 * 清除不在ShipCN内的故障信息
                 */
                faultTypeList.add(l);
            }
        });

        Map<String, Object> resMap = new HashMap<>();
        resMap.put("faults", faultTypeList);
        Map<String, Integer[]> shipMap = new HashMap<>();
        shipTypeList.forEach(l -> {
            Integer[] arr = new Integer[faultTypeList.size()];
            int[] i = new int[]{0};
            faultTypeList.forEach(a -> {
                arr[i[0]] = 0;
                datas.forEach(b -> {
                    if (l.equals(b.get("ship_type")) && a.equals(b.get("fault_type"))) {
                        arr[i[0]] = Integer.parseInt(b.get("count").toString());
                    }
                });
                i[0]++;
            });
            shipMap.put(ShipCN.getCNTrim(l), arr);
        });

        resMap.put("ships", shipMap);
        resMap.put("faultsCN", faultsCN);
        resMap.put("shipsCN", shipsCN);
        return resMap;
    }
}
